package util;


import java.awt.Color;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;


//异常弹窗
public class TimerFrame  extends JFrame implements Runnable {
	private int x;
	private Thread t;
	//设置宽和高
	private int width=430;
	private int height=180;
	 // 得到显示器屏幕的宽高
    public int windowsWedth  = Toolkit.getDefaultToolkit().getScreenSize().width;
    public int windowsHeight  = Toolkit.getDefaultToolkit().getScreenSize().height;
    //设置字体
    private Font font = new Font("微软雅黑", Font.BOLD , 13);
    // 窗口-顶层面板
 	private JDialog jDialog;
 	//回调函数
    private ICallback ic;
    //要提示的内容
    private JLabel jl;
    //设置回调
	public void setIc(ICallback ic) {
		this.ic = ic;
		this.Abnormal();
	}
	public void Abnormal(){
		jDialog= new JDialog();
		jDialog.setBounds((windowsWedth -width ) / 2,(windowsHeight - height  ) / 2, width, height);
		jDialog.setLayout(null);
		jDialog.setAlwaysOnTop(true);
		// 窗口内加入面板
		JPanel jPanel = new JPanel();
		jDialog.setContentPane(jPanel);
		jPanel.setLayout(null);
		jPanel.setBackground(Color.white);
		//提示字
		JLabel jlimg=new JLabel();
		jlimg.setBounds(30, 20, 40, 50);
		jPanel.add(jlimg);
		//创建一个图片组件
		ImageIcon icon=new ImageIcon("image/bonus.png");
		jlimg.setIcon(icon);
		jl=new JLabel();
		jl.setBounds(70, 20, width-20, 50);
		jl.setFont(font);
		jPanel.add(jl);
		t=new Thread(this);
	     t.start();
		//确定按钮
		JButton jb=new JButton("确定");
		jb.setBounds(110, 100, 70, 25);
		jb.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				ic.postExec();
				jDialog.dispose();
				t.stop();
				//System.exit(0);
			}
		});
		jPanel.add(jb);
		//取消按钮
		JButton jb1=new JButton("取消");
		jb1.setBounds(230, 100, 70, 25);
		jb1.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				jDialog.dispose();
				t.stop();
			}
		});
		jPanel.add(jb1);
		jDialog.setModal(true);
		//jDialog.setDefaultCloseOperation(JDialog.EXIT_ON_CLOSE);
		jDialog.setVisible(true);
		jDialog.setResizable(false);
	}
	 public void run()
	    {
	        for(x=5;x>0;x--)
	        {
	        	System.out.println("进入了");
	        	jl.setText("还有(" + Integer.toString(x) + ")自动执行");
	            try {
	                t.sleep(1000);
	            } catch (Exception e) {
	                System.out.println("异常:" + e);
	            }
	        }
	        ic.postExec();
	        jDialog.dispose();
	    }
	
	/*public static void main(String[] args) {
		AbnormalPopUp up=new AbnormalPopUp();
		up.setIc(new ICallback() {
			@Override
			public void postExec() {
				System.out.println("你你你你");
			}
		}, "你大爷的");
	}*/
	
}
